package com.mobileapp.baliairport.src.listFlight;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import com.mobileapp.baliairport.src.entity.listFlight.flight;
import com.mobileapp.baliairport.src.main.MainTabActivity;
import com.mobileapp.baliairport.src.main.R;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class flightList extends Fragment {

	private ArrayList<flight> flightlistdb = new ArrayList<flight>();
	private String[] test = new String[10];
	// { "terbang1", "terbang2", "terbang3", "terbang4" };
	// All static variables
	static final String URL = "http://api.androidhive.info/pizza/?format=xml";
	// XML node keys
	static final String KEY_ITEM = "item"; // parent node
	static final String KEY_ID = "id";
	static final String KEY_NAME = "name";
	static final String KEY_COST = "cost";
	static final String KEY_DESC = "description";

	public flight fldb;

	public ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		/*
		 * XMLParser parser = new XMLParser(); String xml =
		 * parser.getXmlFromUrl(URL); // getting XML Document doc =
		 * parser.getDomElement(xml); // getting DOM element NodeList nl =
		 * doc.getElementsByTagName(KEY_ITEM);
		 */
		/*
		 * for (int i = 0; i < nl.getLength(); i++) { // creating new HashMap
		 * HashMap<String, String> map = new HashMap<String, String>(); Element
		 * e = (Element) nl.item(i); // adding each child node to HashMap key =>
		 * value map.put(KEY_ID, parser.getValue(e, KEY_ID)); map.put(KEY_NAME,
		 * parser.getValue(e, KEY_NAME)); map.put(KEY_COST, "Rs." +
		 * parser.getValue(e, KEY_COST)); map.put(KEY_DESC, parser.getValue(e,
		 * KEY_DESC));
		 * 
		 * // adding HashList to ArrayList menuItems.add(map); }
		 */
		
		
		MainTabActivity.titleback = "FLIGHT HEADER";
		MainTabActivity.bar.setDisplayHomeAsUpEnabled(true);
		
		if(this.flightlistdb.isEmpty()){
			
		}else{
			this.flightlistdb.clear();
		}
		
		for (int i = 0; i < 20; i++) {
			this.fldb = new flight();
			this.fldb.setTerbang("From Kota ke " + i);
			this.flightlistdb.add(this.fldb);
			// test[i] = "terbang"+i;
		}

		View view = inflater.inflate(R.layout.list_flight, container, false);
		ListView listview = (ListView) view.findViewById(R.id.listView1);
		// System.out.println(this.flightlistdb.get(0).getTerbang());
		listview.setAdapter(new MyAdapter(view.getContext()));
		MainTabActivity.bar.setTitle("FLIGHT INFORMATION - DOMESTIC AREA");
		return view;
	}
	
	 @Override
     public boolean onOptionsItemSelected(MenuItem item) {
             switch (item.getItemId()) {
             case android.R.id.home:
                    Toast.makeText(null,"Back button clicked", Toast.LENGTH_SHORT).show(); 

             }
             return true;
     }

	public class MyAdapter extends BaseAdapter {

		private Context mContext;

		// ArrayList<flightList> flightlistdb;

		public MyAdapter(Context context) {
			mContext = context;
		}

		/*
		 * public MyAdapter(Context context, ArrayList<flightList> flightlistdb)
		 * { // TODO Auto-generated constructor stub mContext = context;
		 * this.flightlistdb = flightlistdb; }
		 */

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			// return menuItems.size();
			// return test.length;
			return flightlistdb.size();
			// return test.length;

		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			// return menuItems.get(arg0);
			// return test[arg0];
			return flightlistdb.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View listView;
			if (convertView == null) {
				listView = new View(mContext);
				LayoutInflater inflater = getLayoutInflater(getArguments());
				listView = inflater.inflate(R.layout.list_item_flight, parent, false);
			} else {
				listView = (View) convertView;
			}

			/*
			 * ListAdapter adapter = new SimpleAdapter(this, menuItems,
			 * R.layout.list_item, new String[] { KEY_NAME, KEY_DESC, KEY_COST
			 * }, new int[] { R.id.name, R.id.desciption, R.id.cost });
			 * 
			 * setListAdapter(adapter);
			 */

			TextView txtID = (TextView) listView.findViewById(R.id.name);
			// txtID.setText(menuItems.get(position).get(KEY_NAME));
			// txtID.setText(test[position]);
			txtID.setText(flightlistdb.get(position).getTerbang());

			return listView;

		}

	}

}